package com.atguigu.spzx.cloud.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.util.List;

/**
 * @Author ChenXing
 * @Description
 * @Create 2024/7/23 23:14
 */
@Component
public class MyAuthFilter implements GlobalFilter , Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        System.out.println("自定义过滤器");

        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        RequestPath path = request.getPath();
        URI uri = request.getURI();
        HttpHeaders headers = request.getHeaders();
        List<String> tokens = headers.get("token");

        // 统一鉴权

        System.out.println(path);
        System.out.println(uri);
        System.out.println(tokens);

        return chain.filter(exchange);
    }

    @Override // 值越小，执行的优先级越高，但高不过断言
    public int getOrder() {
        return -1;
    }
}
